package com.example.fwx;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.os.Vibrator;
import android.os.VibrationEffect;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity"; // 添加TAG用于日志

    private Fragment fragment_cal = new Calculate();
    private Fragment fragment_weather = new Weather();
    private Fragment fragment_other2 = new TCP();
    private Fragment fragment_other3 = new SensorFragment();
    private Fragment fragment_image = new ImageRecognitionFragment(); // 修改为图像识别Fragment

    private LinearLayout linearLayout_cal;
    private LinearLayout linearLayout_other1;
    private LinearLayout linearLayout_other2;
    private LinearLayout linearLayout_other3;
    private LinearLayout linearLayout_image; // 修改为图像识别布局

    private ImageView imageView_cal;
    private ImageView imageView_other1;
    private ImageView imageView_other2;
    private ImageView imageView_other3;
    private ImageView imageView_image; // 修改为图像识别图标

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉标题栏
        setContentView(R.layout.activity_main);

        fragmentManager = getSupportFragmentManager();

        initView();
        initFragment();
        initEvent();
        Fragment_select(0);
        //将第一个图标设为选中状态
        imageView_cal.setImageResource(R.drawable.cal1);

        // 添加日志
        Log.d(TAG, "MainActivity初始化完成");
    }

    @Override
    public void onClick(View view) {
        // 添加振动反馈，提升用户体验
        if (Build.VERSION.SDK_INT >= 26) {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(
                    VibrationEffect.createOneShot(30, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(30);
        }

        // 添加日志和Toast用于调试
        Log.d(TAG, "点击事件触发: ID=" + view.getId() + ", image ID=" + R.id.image);

        restartButton();
        int id = view.getId();
        if (id == R.id.cal) {//选择所点击的菜单对应的图层片段
            Fragment_select(0);
            //将该菜单的点击状态置为点击态
            imageView_cal.setImageResource(R.drawable.cal1);
            Log.d(TAG, "选择了计算器");
        } else if (id == R.id.weather) {
            Fragment_select(1);
            imageView_other1.setImageResource(R.drawable.other11);
            Log.d(TAG, "选择了天气");
        } else if (id == R.id.other2) {
            Fragment_select(2);
            imageView_other2.setImageResource(R.drawable.other21);
            Log.d(TAG, "选择了TCP");
        } else if (id == R.id.other3) {
            Fragment_select(3);
            imageView_other3.setImageResource(R.drawable.other31);
            Log.d(TAG, "选择了传感器");
        } else if (id == R.id.image) { // 修改为图像识别点击处理
            Toast.makeText(this, "正在切换到图像识别", Toast.LENGTH_SHORT).show();
            Fragment_select(4);
            imageView_image.setImageResource(R.drawable.image1);
            Log.d(TAG, "选择了图像识别");
        } else {
            Log.e(TAG, "未知的视图ID: " + id);
        }
    }

    private void restartButton() {
        //设置为未点击状态
        imageView_cal.setImageResource(R.drawable.cal2);
        imageView_other1.setImageResource(R.drawable.other12);
        imageView_other2.setImageResource(R.drawable.other22);
        imageView_other3.setImageResource(R.drawable.other32);
        imageView_image.setImageResource(R.drawable.image2); // 修改为图像识别未点击状态
    }

    //初始化中间的部分的图层片段
    private void initFragment(){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.frame_content, fragment_cal);
        transaction.add(R.id.frame_content, fragment_weather);
        transaction.add(R.id.frame_content, fragment_other2);
        transaction.add(R.id.frame_content, fragment_other3);
        transaction.add(R.id.frame_content, fragment_image); // 修改为图像识别Fragment

        //提交事务
        transaction.commit();

        Log.d(TAG, "所有Fragment已初始化");
    }

    //初始化各底端的LinearLayout、ImageView和TextView组件
    private void initView(){
        //改变图标颜色
        linearLayout_cal = findViewById(R.id.cal);
        linearLayout_other1 = findViewById(R.id.weather);
        linearLayout_other2 = findViewById(R.id.other2);
        linearLayout_other3 = findViewById(R.id.other3);

        // 添加日志检查
        linearLayout_image = findViewById(R.id.image);
        if (linearLayout_image == null) {
            Log.e(TAG, "找不到image布局！检查bottom.xml中的ID是否为image");
            Toast.makeText(this, "找不到图像识别按钮布局", Toast.LENGTH_LONG).show();
        } else {
            Log.d(TAG, "成功找到图像识别布局 - ID: " + R.id.image);
        }

        imageView_cal = findViewById(R.id.imageView1);
        imageView_other1 = findViewById(R.id.imageView2);
        imageView_other2 = findViewById(R.id.imageView3);
        imageView_other3 = findViewById(R.id.imageView4);

        // 添加日志检查
        imageView_image = findViewById(R.id.imageView5);
        if (imageView_image == null) {
            Log.e(TAG, "找不到imageView5！检查bottom.xml中的图像ID");
            Toast.makeText(this, "找不到图像识别按钮图标", Toast.LENGTH_LONG).show();
        } else {
            Log.d(TAG, "成功找到图像识别图标");
        }
    }

    //初始化点击监听事件
    private void initEvent(){
        linearLayout_cal.setOnClickListener(this);
        linearLayout_other1.setOnClickListener(this);
        linearLayout_other2.setOnClickListener(this);
        linearLayout_other3.setOnClickListener(this);

        // 设置图像识别按钮监听器
        if (linearLayout_image != null) {
            linearLayout_image.setOnClickListener(this);
            Log.d(TAG, "为图像识别按钮设置了点击监听器");
        }
    }
    //把没有使用的界面的内容隐藏
    private void hideView(FragmentTransaction transaction){
        transaction.hide(fragment_cal);
        transaction.hide(fragment_weather);
        transaction.hide(fragment_other2);
        transaction.hide(fragment_other3);
        transaction.hide(fragment_image); // 修改为隐藏图像识别Fragment
    }

    //显示选中界面的内容，选中界面图标为绿色
    private void Fragment_select(int i){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        //调用隐藏所有图层函数
        hideView(transaction);
        switch (i){
            case 0:
                transaction.show(fragment_cal);
                break;
            case 1:
                transaction.show(fragment_weather);
                break;
            case 2:
                transaction.show(fragment_other2);
                break;
            case 3:
                transaction.show(fragment_other3);
                break;
            case 4: // 修改为图像识别选择
                transaction.show(fragment_image);
                Log.d(TAG, "显示图像识别Fragment");
                break;
            default:
                break;
        }
        //提交转换事务
        transaction.commit();
    }
}